/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.plan.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.javaeye.common.service.ICodeService;
import com.javaeye.common.util.ListItem;
import com.javaeye.common.util.ListUtil;
import com.javaeye.common.web.PageInfo;
import com.javaeye.delivery.dto.OrderDetailBatchInfo;
import com.javaeye.delivery.service.OrderDetailBatchInfoService;
import com.javaeye.plan.dto.MaterialRequisiteForm;
import com.javaeye.plan.dto.MaterialRequisiteFormDetail;
import com.javaeye.plan.service.IMaterialRequisiteFormService;
import com.javaeye.store.dto.Materials;
import com.javaeye.store.dto.MaterialsBatchDetail;
import com.javaeye.store.service.IMaterialsBatchInfoService;
import com.javaeye.store.service.IMaterialsService;

public class MaterialRequisiteFormAction extends ActionSupport {
	
	protected static Log log = LogFactory.getLog(MaterialRequisiteFormAction.class); 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3216585574200112708L;

	/**
	 * 用于显示一览结果集
	 */
	private List<MaterialRequisiteForm> formList;
	
	/**
	 * 用于接收分页值对象
	 */
	private PageInfo pageInfo;
	
	/**
	 * 用于接收查询条件值对象
	 */
	private VoucherCondition condition;
	
	/**
	 * 用于接收添加画面和编辑画面的值对象
	 */
	private MaterialRequisiteForm mrForm;
	
	/**
	 * 用于接收需要修改和删除的ID值
	 */
	private String formId;
	
	/**
	 * 用于保存在库品编码
	 */
	private List<String> materialCodes;
	
	/**
	 * 用于保存在库品分类名称
	 */
	private List<String> rootCategoryNames;
	
	/**
	 * 用于保存在库品分类名称
	 */
	private List<String> categoryNames;
	
	/**
	 * 用于保存在库品送检数量
	 */
	private List<Float> materialNums;
	
	/**
	 * 用于生成查询画面上选择单位的下拉框的值
	 */
	private List<ListItem> departmentList;
	
	private List<Integer> detailIds;
	
	private List<Float> outStroeNumbers;
	
	private List<String> outStroeBatchNos;
	
	private List<Integer> materialIds;
	
	private List<String> materialSkus;
	
	/**
	 * Service
	 */
	protected IMaterialRequisiteFormService service;
	
	private IMaterialsService materialsService;
	
	protected ICodeService codeService;
	
	private IMaterialsBatchInfoService batchInfoService;
	
	protected OrderDetailBatchInfoService orderDetailBatchInfoService;
	
	public void setOrderDetailBatchInfoService(OrderDetailBatchInfoService orderDetailBatchInfoService) {
		this.orderDetailBatchInfoService = orderDetailBatchInfoService;
	}
	
	public void setBatchInfoService(IMaterialsBatchInfoService batchInfoService) {
		this.batchInfoService = batchInfoService;
	}
	
	public void setService(IMaterialRequisiteFormService service) {
		this.service = service;
	}
	
	public void setCodeService(ICodeService codeService) {
		this.codeService = codeService;
	}

	public void setMaterialsService(IMaterialsService materialsService) {
		this.materialsService = materialsService;
	}

	public MaterialRequisiteFormAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new VoucherCondition();
		condition.setBeginDate(null);
		condition.setEndDate(null);
		condition.setType(-1);
		condition.setStatus(-1);
		condition.setMaterialsName("");
		condition.setBatchNo("");
		condition.setVoucherNo("");
		condition.setSkuId(-1);
	}
	
	public String queryMaterialRequisiteFormList() throws Exception {
		departmentList = ListUtil.baseCodeList(codeService, ListUtil.UNIT_CODE_DEPATMENT);
		formList = service.getFormList(condition, pageInfo);
		return SUCCESS;
	}
	
	public String queryMaterialRequisiteFormOutStoreList() throws Exception {
		condition.setStatus(MaterialRequisiteForm.MATERIAL_REQUISITE_FORM_STATUS_CREATE);
		condition.setType(MaterialRequisiteForm.FORM_TYPE_REQUISITE);
		return queryMaterialRequisiteFormList();
	}
	
	public String materialRequisiteFormOutStore() throws Exception {
		//批次出库信息
		List<OrderDetailBatchInfo> batchInfos = new ArrayList<OrderDetailBatchInfo>();
		for (int i = 0; i < outStroeNumbers.size(); i++) {
			float number = outStroeNumbers.get(i);
			if (number > 0) {
				OrderDetailBatchInfo orderDetailBatchInfo = new OrderDetailBatchInfo();
				orderDetailBatchInfo.setOrderId(formId);
				orderDetailBatchInfo.setOrderDetailId(detailIds.get(i));
				orderDetailBatchInfo.setProductId(materialIds.get(i));
				orderDetailBatchInfo.setBatchNo(outStroeBatchNos.get(i));
				orderDetailBatchInfo.setNumber(number);
				batchInfos.add(orderDetailBatchInfo);
			}
		}
		
		service.materialRequisiteFormOutStore(formId, batchInfos);
		return SUCCESS;
	}
	
	public String queryForAddForm() throws Exception {
		departmentList = ListUtil.baseCodeList(codeService, ListUtil.UNIT_CODE_DEPATMENT);
		return SUCCESS;
	}
	
	public String queryMaterialRequisiteFormInfo() throws Exception {
		mrForm = service.getMaterialRequisiteForm(formId);
		Set<MaterialRequisiteFormDetail> details = mrForm.getFormDetails();
		initFormDetail(formId, details);
		return SUCCESS;
	}
	
	protected void initFormDetail(String formBatchId, Set<MaterialRequisiteFormDetail> details) {
		for (MaterialRequisiteFormDetail detail : details) {
			List<OrderDetailBatchInfo> batchInfos = 
				orderDetailBatchInfoService.getBatchInfo(formBatchId, detail.getMaterialId());
			List<MaterialsBatchDetail> myBatchInfos = new ArrayList<MaterialsBatchDetail>();
			for (OrderDetailBatchInfo info : batchInfos) {
				MaterialsBatchDetail batchInfo = new MaterialsBatchDetail();
				batchInfo.setBatchNo(info.getBatchNo());
				batchInfo.setNumber(info.getNumber());
				myBatchInfos.add(batchInfo);
			}
			detail.setBatchInfos(myBatchInfos);
		}
	}
	
	public String queryMaterialRequisiteFormForEdit() throws Exception {
		departmentList = ListUtil.baseCodeList(codeService, ListUtil.UNIT_CODE_DEPATMENT);
		mrForm = service.getMaterialRequisiteForm(formId);
		return SUCCESS;
	}
	
	public String removeMaterialRequisiteForm() throws Exception {
		service.removeMaterialRequisiteForm(formId);
		return SUCCESS;
	}
	
	public String queryMaterialRequisiteFormOutStroe() throws Exception {
		mrForm = service.getMaterialRequisiteForm(formId);
		Set<MaterialRequisiteFormDetail> details = mrForm.getFormDetails();
		for (MaterialRequisiteFormDetail detail : details) {
			List<MaterialsBatchDetail> batchInfos = 
				batchInfoService.getNotEmptyMeterialsBatchInfo(detail.getMaterialId());
			
			float number = detail.getNumber();
			List<MaterialsBatchDetail> computerInfos = new ArrayList<MaterialsBatchDetail>();
			for (MaterialsBatchDetail info : batchInfos) {
				if (number > 0) {
					float storeNumber = info.getInNumber() - info.getOutNumber();
					float outNumber = (storeNumber < number) ? storeNumber : number;
					number -= outNumber;
					MaterialsBatchDetail batchInfo = new MaterialsBatchDetail();
					batchInfo.setBatchNo(info.getBatchNo());
					batchInfo.setNumber(storeNumber);
					batchInfo.setOutNumber(outNumber);
					computerInfos.add(batchInfo);
				}
			}
			detail.setBatchInfos(computerInfos);
		}
		return SUCCESS;
	}
	
	/**
	 * 保存
	 * @return
	 * @throws Exception
	 */
	public String saveMaterialRequisiteForm() throws Exception {
		mrForm.setDeptName(ListUtil.baseCodeName(codeService, String.valueOf(mrForm.getDeptId())));
		for (int i = 0; i < materialCodes.size(); i++) {
			String materialCode = materialCodes.get(i);
			Materials materials = materialsService.getMaterialsByCode(materialCode);
			
			MaterialRequisiteFormDetail detail = new MaterialRequisiteFormDetail();
			detail.setMaterialId(materialIds.get(i));
			detail.setMaterialNo(materialCode);
			detail.setMaterialRootCategory(rootCategoryNames.get(i));
			detail.setMaterialCategory(categoryNames.get(i));
			detail.setMaterialName(materials.getName());
			detail.setMaterialType(materials.getType());
			detail.setUnit(materials.getUnits());
			detail.setMaterialSku(materialSkus.get(i));
			detail.setNumber(materialNums.get(i));

			if (detailIds != null && detailIds.size() > 0) {
				if (i < detailIds.size()) {
					detail.setId(detailIds.get(i));
				}
			}
			
			mrForm.addDetail(detail);
		}
		service.saveMaterialRequisiteForm(mrForm);
		return SUCCESS;
	}

	public List<MaterialRequisiteForm> getFormList() {
		return formList;
	}

	public void setFormList(List<MaterialRequisiteForm> formList) {
		this.formList = formList;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public VoucherCondition getCondition() {
		return condition;
	}

	public void setCondition(VoucherCondition condition) {
		this.condition = condition;
	}

	public MaterialRequisiteForm getMrForm() {
		return mrForm;
	}

	public void setMrForm(MaterialRequisiteForm mrForm) {
		this.mrForm = mrForm;
	}

	public String getFormId() {
		return formId;
	}

	public void setFormId(String formId) {
		this.formId = formId;
	}

	public List<String> getMaterialCodes() {
		return materialCodes;
	}

	public void setMaterialCodes(List<String> materialCodes) {
		this.materialCodes = materialCodes;
	}

	public List<String> getRootCategoryNames() {
		return rootCategoryNames;
	}

	public void setRootCategoryNames(List<String> rootCategoryNames) {
		this.rootCategoryNames = rootCategoryNames;
	}

	public List<String> getCategoryNames() {
		return categoryNames;
	}

	public void setCategoryNames(List<String> categoryNames) {
		this.categoryNames = categoryNames;
	}

	public List<Float> getMaterialNums() {
		return materialNums;
	}

	public void setMaterialNums(List<Float> materialNums) {
		this.materialNums = materialNums;
	}

	public List<ListItem> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<ListItem> departmentList) {
		this.departmentList = departmentList;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public List<Float> getOutStroeNumbers() {
		return outStroeNumbers;
	}

	public void setOutStroeNumbers(List<Float> outStroeNumbers) {
		this.outStroeNumbers = outStroeNumbers;
	}

	public List<String> getOutStroeBatchNos() {
		return outStroeBatchNos;
	}

	public void setOutStroeBatchNos(List<String> outStroeBatchNos) {
		this.outStroeBatchNos = outStroeBatchNos;
	}

	public void setMaterialIds(List<Integer> materialIds) {
		this.materialIds = materialIds;
	}

	public List<Integer> getMaterialIds() {
		return materialIds;
	}

	public List<String> getMaterialSkus() {
		return materialSkus;
	}

	public void setMaterialSkus(List<String> materialSkus) {
		this.materialSkus = materialSkus;
	}
	
}
